from otree.api import * doc = """ This is a standard 2-player trust game where the amount sent by player 1 gets tripled. The trust game was first proposed by Berg, Dickhaut, and McCabe (1995) . """ class Constants(BaseConstants): name_in_url = 'trust' players_per_group = 2 num_rounds = 16 instructions_template = 'trust/instructions.html' # Initial amount allocated to each player endowment = cu(8) multiplier = 1/2 principal_role = 'A' agent_role = 'B' class Subsession(BaseSubsession): def creating_session(self): if self.round_number < 10: self.group_randomly(fixed_id_in_group=True) else: self.group_like_round(9) class Group(BaseGroup): sent_amount = models.CurrencyField( min=0, max=Constants.endowment, doc="""Amount sent by A""", label="Please enter an amount from 0 to 8:", ) sent_back_amount = models.CurrencyField(doc="""Amount sent back by B""", min=cu(0)) level = models.IntegerField( choices=['accept', 'reject'], widget=widgets.RadioSelect ) class Player(BasePlayer): total_payoff = models.Currencyfield() sent_amount = models.CurrencyField(doc="""Amount sent by A""",label="Please enter an amount from 0 to 8:",) sent_back_amount = models.CurrencyField(doc="""Amount sent by B""",label="Please enter an amount from 0 to 4:",) offer_accepted = models.BooleanField() # FUNCTIONS def sent_back_amount_max(group: Group): return Constants.endowment * Constants.multiplier def set_payoffs(group: Group): A = group.get_player_by_id(1) B = group.get_player_by_id(2) if B_chooses_to_accect_in_decision2: A.payoff = Constants.endowment - group.sent_amount B.payoff = group.sent_amount if A_chooses_to_accect_in_decision4: A.payoff = group.sent_back_amount B.payoff = Constants.endowment * Constants.multiplier - group.sent_back_amount if A_chooses_to_reject_in_decision4: A.payoff = 0 B.payoff = 0 # PAGES class Instruction(Page): def is_displayed(self): return self.round_number == 1 class Introduction(Page): timeout_seconds = 60 @staticmethod def before_next_page(player, timeout_happened): if timeout_happened: player.xyz = True class Decision1(Page): timeout_seconds = 60 @staticmethod def before_next_page(player, timeout_happened): if timeout_happened: player.xyz = True """This page is only for A sends amount (all, some, or none) to B This amount is, i.e if sent amount by A is 5, amount received by B is 5""" form_model = 'group' form_fields = ['sent_amount'] @staticmethod def is_displayed(player: Player): return player.id_in_group == 1 class DecisionWaitPage(WaitPage): class MyWaitPage(WaitPage): after_all_players_arrive = set_payoffs class Decision2(Page): timeout_seconds = 60 @staticmethod def before_next_page(player, timeout_happened): if timeout_happened: player.xyz = True """This page is only for B B choice whether to accept or reject A's proposal.""" form_model = 'group' form_fields = ['offer_accepted'] @staticmethod def is_displayed(player: Player): return player.id_in_group == 2 @staticmethod def app_after_this_page(player, upcoming_pages): if player_choices_accept: return upcoming_pages[-1] class Introduction2(Page): timeout_seconds = 60 @staticmethod def before_next_page(player, timeout_happened): if timeout_happened: player.xyz = True class Decision3(Page): timeout_seconds = 60 @staticmethod def before_next_page(player, timeout_happened): if timeout_happened: player.xyz = True """This page is only for B B sends amount (all, some, or none) to A This amount is tripled by experimenter, i.e if sent amount by B is 5, amount received by A is 5""" form_model = 'group' form_fields = ['sent_back_amount'] @staticmethod def is_displayed(player: Player): return player.id_in_group == 2 class DecisionWaitPage2(WaitPage): class MyWaitPage(WaitPage): after_all_players_arrive = set_payoffs class Decision4(Page): timeout_seconds = 60 @staticmethod def before_next_page(player, timeout_happened): if timeout_happened: player.xyz = True """This page is only for B B choice whether to accept or reject A's proposal.""" form_model = 'group' form_fields = ['offer_accepted'] @staticmethod def is_displayed(player: Player): return player.id_in_group == 1 class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs class Results(Page): """This page displays the earnings of each player""" page_sequence = [ Instruction, Introduction, Decision1, DecisionWaitPage, Decision2, Introduction2, Decision3, DecisionWaitPage2, Decision4, ResultsWaitPage, Results, ]